package a51qindian.com.comprehensive.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.li.widget.CircleImageView;

import java.util.HashMap;
import java.util.Map;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.activity.PersonalDetailActivity;
import a51qindian.com.comprehensive.activity.SettingActivity;
import a51qindian.com.comprehensive.base.QDBaseFragment;
import a51qindian.com.comprehensive.model.BaseModel;
import a51qindian.com.comprehensive.model.UserDetailInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import a51qindian.com.comprehensive.util.SpUtil;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/9/12 0012
 * Description: "我"
 */

public class MineFragment extends QDBaseFragment {

    @BindView(R.id.img_avatar)
    CircleImageView mImgAvatar;
    @BindView(R.id.txt_name)
    TextView mTxtName;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_city_mine;
    }


    @Override
    public void onResume() {
        super.onResume();
        getUserInfo(SpUtil.getUserInfo(mContext, SpUtil.USER_ID), "android");

    }

    @OnClick({R.id.img_avatar, R.id.txt_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_setting:

                startActivity(new Intent(mContext, SettingActivity.class));
                break;
            case R.id.img_avatar:
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
                        ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    startActivity(new Intent(mContext, PersonalDetailActivity.class));

                }else {
                    ActivityCompat.requestPermissions(getActivity(),new String[]{
                            Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE
                    },1);
                }
                break;
        }

    }

    private void getUserInfo(String uid, String request) {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("request", request);
        ObservableHelper.createObservable(RetrofitCreator.createService().userDetailInfo(map))
                .subscribe(new SelectorObserver<BaseModel<UserDetailInfo>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {

                    }

                    @Override
                    protected void onSuccess(BaseModel<UserDetailInfo> userDetailInfoBaseModel) {
                        UserDetailInfo detailInfo = userDetailInfoBaseModel.getData();
                        RequestOptions options = new RequestOptions();
                        options.placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher);
                        Glide.with(mContext).load(detailInfo.getAvatar()).apply(options).into(mImgAvatar);

                        if (!TextUtils.isEmpty(detailInfo.getRealname())) {
                            mTxtName.setText(detailInfo.getRealname());
                        } else {
                            mTxtName.setText(detailInfo.getUsername());
                        }

                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }
                });
    }

}
